package com.etrans.web.gps.business.gis.service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.gis.domain.CustomLine;
import com.etrans.web.gps.business.gis.domain.CustomLinePoint;
import com.etrans.web.gps.util.DistanceUtil;

@Service
public class CustomLineService extends BaseService {


	/**
	 * @param contactperson
	 */
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,
			Exception.class })
	public void saveCustomLine(CustomLine customLine) {
		Integer key = (Integer) this.insert("customLineMapper.saveCustomLine", customLine);
		if (key > 0) {
			if (!"".equals(customLine.getCustomLinePointStr())) {
				double mealige=0.0;
				double totalmealige=0.0;
				ArrayList<double[]> lnglat= new ArrayList<double[]>();
				String customLinePointStr = customLine.getCustomLinePointStr();
				String[] clpList = customLinePointStr.split(",");
				List<CustomLinePoint> customLinePointList = new ArrayList<CustomLinePoint>();
				for (int i = 0; i < clpList.length; i++) {
					String cplStr = clpList[i];
					String[] poit = cplStr.split("\\|");
					double []n=new double[2];
					CustomLinePoint customLinePoint = new CustomLinePoint();
					customLinePoint.setLineID(customLine.getId());
					customLinePoint.setFlag(0);
					customLinePoint.setZoom(11);
					customLinePoint.setLongitude(poit[0].toString().trim());
					customLinePoint.setLatitude(poit[1].toString().trim());
					customLinePoint.setOriginlongitude(poit[0].toString().trim());
					customLinePoint.setOriginlatitude(poit[1].toString().trim());
					n[0]=(Double.parseDouble(poit[0].toString().trim()));
					n[1]=(Double.parseDouble(poit[1].toString().trim()));
					lnglat.add(n);
					// 将百度经纬度转化为gps经纬度存库
					/*String[] lonlat = BaiDuConvert.BaiduToGps(
							customLinePoint.getLongitude(),
							customLinePoint.getLatitude());
					if (lonlat.length > 1) {
						customLinePoint.setOriginlongitude(lonlat[0]);
						customLinePoint.setOriginlatitude(lonlat[1]);
					}*/

					customLinePointList.add(customLinePoint);
				}
				

				if(lnglat.size()>2){
					for(int i=1;i<lnglat.size();i++){
						mealige=DistanceUtil.GetDistance(lnglat.get(i-1)[1], lnglat.get(i-1)[0], lnglat.get(i)[1], lnglat.get(i)[0]);
						if(mealige>1){
							totalmealige+=mealige;
						}
					}
				}
				

				if(totalmealige>1){
					DecimalFormat    df   = new DecimalFormat("######0.00");
					customLine.setMileage(df.format(1+totalmealige/1000));
					this.update("customLineMapper.updateCustomLine", customLine);
				}
				
				
				this.insert("customLineMapper.saveCustomLinePoint", customLinePointList);
			}
		}

	}
	
	
	/**
	 * @param contactperson
	 */
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,
			Exception.class })
	public void saveCustomTrackLine(CustomLine customLine) {
		Integer key = (Integer) this.insert("customLineMapper.saveCustomLine", customLine);
		if (key > 0) {
			if (!"".equals(customLine.getCustomLinePointStr())) {
				double mealige=0.0;
				double totalmealige=0.0;
				ArrayList<double[]> lnglat= new ArrayList<double[]>();
				List<CustomLinePoint> customLinePointList = new ArrayList<CustomLinePoint>();
				for (int i = 0; i < customLine.getCustomLinePointList().size(); i++) {
					double []n=new double[2];
					CustomLinePoint customLinePoint = customLine.getCustomLinePointList().get(i);
					customLinePoint.setLineID(customLine.getId());
					customLinePoint.setFlag(0);
					customLinePoint.setZoom(11);
					n[0]=(Double.parseDouble(customLinePoint.getLongitude().toString().trim()));
					n[1]=(Double.parseDouble(customLinePoint.getLatitude().toString().trim()));
					lnglat.add(n);
					// 将百度经纬度转化为gps经纬度存库
					/*String[] lonlat = BaiDuConvert.BaiduToGps(
							customLinePoint.getLongitude(),
							customLinePoint.getLatitude());
					if (lonlat.length > 1) {
						customLinePoint.setOriginlongitude(lonlat[0]);
						customLinePoint.setOriginlatitude(lonlat[1]);
					}*/

					customLinePointList.add(customLinePoint);
				}
				

//				if(lnglat.size()>2){
//					for(int i=1;i<lnglat.size();i++){
//						mealige=DistanceUtil.GetDistance(lnglat.get(i-1)[1], lnglat.get(i-1)[0], lnglat.get(i)[1], lnglat.get(i)[0]);
//						if(mealige>1){
//							totalmealige+=mealige;
//						}
//					}
//				}
//				
//
//				if(totalmealige>1){
//					DecimalFormat    df   = new DecimalFormat("######0.00");
//					customLine.setMileage(df.format(1+totalmealige/1000));
//					this.update("customLineMapper.updateCustomLine", customLine);
//				}
				
				
				this.insert("customLineMapper.saveCustomLinePoint", customLinePointList);
			}
		}

	}
	
	/**
	 * 判断重复
	 * 
	 * @param param  
	 * @return
	 */
	public boolean queryisReCustomMap(HashMap<String, Object> param) {
		boolean result=false;
		List<HashMap<String, Object>> list =	this.selectList("customLineQuery.queryisReCustomLine", param);
		if(null!=list&&list.size()>0){
			result=true;
		}
		return result ; 
	} 

	/**
	 * @param contactperson
	 */
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,
			Exception.class })
	public void updateCustomLine(CustomLine CustomLine) {
		this.update("customLineMapper.updateCustomLine", CustomLine);
	}

	/**
	 * @param id
	 */
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,
			Exception.class })
	public void deleteCustomLineByID(Integer id) {
		this.delete("customLineMapper.deleteCustomLineByID", id);// 删除自定义线
		this.delete("customLineMapper.deleteCustomLinePoint", id);// 删除自定义线

	}

	/**
	 * @param id
	 * @return
	 */
	public CustomLine queryCustomLineById(Integer id) {
		CustomLine CustomLine = (CustomLine) this.selectOne(
				"customLineQuery.queryCustomLineById", id);
		return CustomLine;
	}

	public List<CustomLinePoint> queryAllCustomLinePointById(Integer lineID) {
		return   this.selectList(
				"customLineQuery.queryAllCustomLinePointById", lineID);
	}

	public List<CustomLine> queryAllCustomLine(HashMap<String, Object> param) {
		return   this.selectList("customLineQuery.queryAllCustomLine", param);
	}

	public long queryCustomLineCount(HashMap<String, Object> param) {
		return (Long) this.selectOne("customLineQuery.queryCustomLineCount", param);
	}
	
	public long queryCustomLineCountByCoverageGroups(Integer CoverageGroupsID) {
		return (Long) this.selectOne("customLineQuery.queryCustomLineCountByCoverageGroups", CoverageGroupsID);
	}

}
